package com.zu.commu.auto1_0.protocol.call;

import com.zu.commu.auto1_0.DataType1_0;
import com.zu.commu.auto1_0.protocol.CheckData;
import com.zu.commu.auto1_0.protocol.Util;
import com.zu.commu.exception.CommuException;
import com.zu.commu.interf.Data;
import com.zu.commu.util.DateTime;

public class ReportDataProtocol2018 {
	public Data analyseReportData(byte[] b) throws Exception {
		new CheckData().checkCrc(b, 4, 27, 28);
		Data d =new Data();
		//获取水表编号
		getMeterNo(b, d);
		//获取状态值
		getStatus(b, d);
		// 剩余水量
		getRemainWater(b, d);
		// 累计取水量
		getAccumulateFlux(b, d);
		// 瞬时流量
		getInstantFlux(b, d);
		// 充值时间
		getChargeTimes(b, d);
		// 水位
		getWaterLevel(b, d);

		d.setDateTime(DateTime.yymmddhhmmss());
		if (b[11] == -50) {
			d.setDataType(DataType1_0.SETCLOCK.toInt() + "");
			return d;
		}
		d.setDataType(DataType1_0.REPORTDATA.toInt() + "");
		return d;
	}

	private void getMeterNo(byte[] b, Data d) throws Exception {
		try {
			Util u = new Util();
			d.setMeterNo(u.byte2Hex(b, 4, 9));
		} catch (Exception e) {
			throw new CommuException().setMessage(
					"从上报数据中读取测控器地址号时出错！" + e.getMessage(), null);
		}
	}

	private void getStatus(byte[] b, Data d) throws Exception {
		try {
			Util u = new Util();
			String s = u.byte2Binary(b[12]);
			while (s.length() < 8) {
				s = "0" + s;
			}
			d.setSensorStatus(s.charAt(0) + "");
			d.setPowerStatus(s.charAt(1) + "");
			d.setPumpStatus(s.charAt(2) + "");
			d.setDoorStatus(s.charAt(3) + "");
			d.setRemainWaterStatus(s.charAt(4) + "");
		} catch (Exception e) {
			throw new CommuException().setMessage(
					"从上报数据中读取各种状态时出错！" + e.getMessage(), null);
		}
	}

	// 剩余水量->电池电压
	private void getRemainWater(byte[] b, Data d) throws Exception {
		Util u = new Util();
		String minus = "";
		try {
			String s = u.byte2Binary(b[13]);
			if (s.length() > 4) {
				minus = "-";
				byte[] tmp38_35 = b;
				tmp38_35[13] = (byte) (tmp38_35[13] - 16);
			}
			Double sl = u.string2Double(minus+ u.clean0FromStrNum(u.byte2Hex(b, 13, 16)));
			d.setRemainWater(sl/100);//对应电池电压
		} catch (Exception e) {
			throw new CommuException().setMessage(
					"从上报数据中读取剩余水量时出错！" + e.getMessage(), null);
		}
	}

	// 累计取水量->信号强度
	private void getAccumulateFlux(byte[] b, Data d) throws Exception {
		try {
			Util u = new Util();
			double dd = u.string2Double(u.clean0FromStrNum(u
					.byte2Hex(b, 17, 20)));
			if (dd > 10000000.0D) {
				dd = 10000000.0D - dd;
			}
			d.setAccumulateFlux(Double.valueOf(dd));
		} catch (Exception e) {
			throw new CommuException().setMessage(
					"从上报数据中读取累积用水量时出错！" + e.getMessage(), null);
		}
	}

	// 瞬时流量->探头埋深
	private void getInstantFlux(byte[] b, Data d) throws Exception {
		Util u = new Util();
	     try {
	    	 String temp = u.byte2Hex(b, 21, 22);
	    	 Double valueOf = Double.valueOf(temp)/100;
	       d.setInstantFlux(valueOf);       
	     } catch (Exception e) {
			throw new CommuException().setMessage(
					"从上报数据中读取剩余水量时出错！" + e.getMessage(), null);
		}
	}

	// 充值时间-> 传感器电流
	private void getChargeTimes(byte[] b, Data d) throws Exception {
		try {
			Util u = new Util();
			d.setChargeTimes(Integer.valueOf(u.string2Int(u.clean0FromStrNum(u
					.byte2Hex(b, 23, 24)))));
		} catch (Exception e) {
			throw new CommuException().setMessage(
					"从上报数据中读取充值次数时出错！" + e.getMessage(), null);
		}
		
	}

	//水位-设备计算结果
	private void getWaterLevel(byte[] b, Data d) throws Exception {
		try {
			Util u = new Util();
			String hight = u.clean0FromStrNum(u.byte2Hex(b, 25, 26));
			String low = u.clean0FromStrNum(u.byte2Hex(b, 27, 27));
			d.setWaterLevel(Double.valueOf(u.string2Double(hight + "." + low)));
		} catch (Exception e) {
			throw new CommuException().setMessage(
					"从上报数据中读取水位时出错！" + e.getMessage(), null);
		}
	}
}
